package gw.com.android.ui.home;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.gwtsz.gts2.hx.R;

public class CircleWithShadow extends View {

    public CircleWithShadow(Context context) {
        this(context, null);
    }

    public CircleWithShadow(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleWithShadow(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dealAttr(context, attrs);
        initPaint();
    }

    private void dealAttr(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleWithShadow);

        if (ta != null) {
            try {
                shadowRadius = ta.getDimension(R.styleable.CircleWithShadow_shadowRadius, 0);
                shadowOffsetY = ta.getDimension(R.styleable.CircleWithShadow_shadowOffsetY, 0);
                shadowOffsetX = ta.getDimension(R.styleable.CircleWithShadow_shadowOffsetX, 0);
                shadowColor = ta.getColor(R.styleable.CircleWithShadow_shadowColor, 0x00FE6665);
                circleFillColor = ta.getColor(R.styleable.CircleWithShadow_circleFillColor, 0x00FE6665);
                circleRadius = ta.getDimension(R.styleable.CircleWithShadow_circleRadius, 0);
            } catch (Exception e) {

            } finally {
                ta.recycle();
            }
        }

    }


    private int minWidth = 50, minHeight = 50;

    /**
     * 重写onMeasure设定最小宽高
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(minWidth, minHeight);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(minWidth, heightSize);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSize, minHeight);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        canvas.drawCircle(width / 2, height / 2, circleRadius, paint);
    }

    private Paint paint;
    private float shadowRadius;
    private float shadowOffsetY;
    private float shadowOffsetX;

    public void setColors(int circleFillColor, int shadowColor) {
        this.shadowColor = shadowColor;
        this.circleFillColor = circleFillColor;
        initPaint();
        postInvalidate();
    }


    private int shadowColor;
    private int circleFillColor;
    private float circleRadius;

    private void initPaint() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);//比如关闭硬件加速，阴影才能生效
        paint = new Paint();
        paint.setColor(circleFillColor);
        paint.setAntiAlias(true);//抗锯齿
        paint.setStyle(Paint.Style.FILL);//FILL填充,stroke描边
        paint.setShadowLayer(shadowRadius, shadowOffsetX, shadowOffsetY, shadowColor);//好，把这里的颜色，变成透明度60%的同种颜色
    }
}

